<%@page import="java.nio.charset.Charset"%>
<%@page import="java.nio.charset.CharsetEncoder"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page import="java.util.GregorianCalendar,java.util.Calendar,java.util.regex.*,java.util.*" %>
<%@page import="java.rmi.registry.LocateRegistry"%>
<%@page import="java.rmi.registry.Registry"%>
<%@page import="java.util.ArrayList"%>
<%@page import="rmi.*" %>
<%@page import="data.*" %>
<jsp:useBean id="manager" scope="session" class="manager.Manager" />
<%
boolean err = false;
String nom = "";
String errNom = "";
String description = "";
String errDescription = "";
String quantite = "";
String errQuantite = "";
String frequence = "";
String errFrequence = "";
String administration = "";
String errAdministration = "";
String categorieTherapeutique="" ;
String errCategorieTherapeutique="" ;

String[] tabAdministration={"Administration orale","Suppositoire","Timbre transdermique","Intraveineuse","Sous-cutanee","Intradermique","Intramusculaire","Pommade","Aerosol","Collyre"} ;
String[] tabCategorieTherapeutique={"Anesthesiants","Analgesiques","Antibiotiques","Antidepresseurs","Anti-diuretiques","Anti-inflammatoires","Antihistaminiques","Anti-hypertenseurs","Antipyretiques","Antiviraux","Antiretroviraux","Antitussifs","Anxiolytiques","Bronchodilatateurs","Diuretiques","Laxatifs","Psychotropes","Sedatifs","Vasopresseurs"} ;

String errAjout="" ;

Pattern pat ;
Matcher m;

if(request.getParameter("submit") != null){ 
	
	nom = request.getParameter("nom");
	if(nom == null) nom = "";
	if(nom.trim().equals("")){
		errNom = "Veuillez remplir le champ nom";
		err = true;
	}
	
	description = request.getParameter("description");
	if(description == null) description = "";
	if(description.trim().equals("")){
		errDescription = "Veuillez remplir le champ description";
		err = true;
	}
	
	quantite = request.getParameter("quantite");
	if(quantite == null) quantite = "";
	if(quantite.trim().equals("")){
		errQuantite = "Veuillez remplir le champ quantité";
		err = true;
	}

	frequence = request.getParameter("frequence");
	if(frequence == null) frequence = "";
	if(frequence.trim().equals("")){
		errFrequence = "Veuillez remplir le champ fréquence";
		err = true;
	}
	
	administration = request.getParameter("administration");
	if(administration == null) administration = "";
	if(administration.trim().equals("")){
		errAdministration = "Veuillez remplir le champ administration";
		err = true;
	}
	
	categorieTherapeutique = request.getParameter("categorieTherapeutique");
	if(categorieTherapeutique == null) categorieTherapeutique = "";
	if(categorieTherapeutique.trim().equals("")){
		errCategorieTherapeutique = "Veuillez remplir le champ categorie thérapeutique";
		err = true;
	}
	if(!err){
		if(manager.getServeurRmi().stocker(new Medicament(nom,description,quantite,frequence,administration,categorieTherapeutique))>0) {
			ArrayList<String> messages = new ArrayList<String>();
			messages.add("Médicament créé");
			manager.setMessages(messages);
			%>
			<jsp:forward page="lister_medicament.jsp" />
<%
			return;
		}else{
			errAjout = "Insertion en bdd impossible";
		}
	}
}
%>

<div id="contenu_style2_haut">
</div>
<div id="contenu_style2_centre">
<form action="creer_medicament.jsp" method="post">
			<table> 
				<tr> 
					<td style="text-align:center;font-weight:bold;width:200px;">Nom * :</td> 
					<td style="text-align:left;"><input type="text" name="nom" size="40" maxlength="255" value="<%= nom %>"/></td> 
					<td>&nbsp;</td>
				</tr> 
<%
				if(!errNom.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errNom %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%>
				<tr> 
					<td style="text-align:center;font-weight:bold;">Description * :</td> 
					<td style="text-align:left;"><textarea name="description" rows="10" cols="40"><%= description %></textarea></td> 
					<td>&nbsp;</td>
				</tr>
<%
				if(!errDescription.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errDescription %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%> 
				<tr> 
					<td style="text-align:center;font-weight:bold;">Quantite * :</td> 
					<td style="text-align:left;"><input type="text" name="quantite" size="50" maxlength="255" value="<%= quantite %>"/></td> 
					<td>&nbsp;</td>
				</tr>
<%
				if(!errQuantite.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errQuantite %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%>
				<tr> 
					<td style="text-align:center;font-weight:bold;">Fréquence * :</td> 
					<td style="text-align:left;"><input type="text" name="frequence" size="50" maxlength="255" value="<%= frequence %>"/></td> 
					<td>&nbsp;</td>
				</tr>
<%
				if(!errFrequence.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errFrequence %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%>
				<tr> 
					<td style="text-align:center;font-weight:bold;">Administration * :</td> 
					<td style="text-align:left;">
						<select name="administration">
							<option value=""></option>
<%
							for(int i=0;i<tabAdministration.length;i++) {
								if(administration.trim().equals(tabAdministration[i].toString())) {
%>
									<option value="<%= tabAdministration[i].toString() %>" selected="selected"><%= tabAdministration[i].toString() %></option>
<%									
								}else{
%>
									<option value="<%= tabAdministration[i].toString() %>"><%= tabAdministration[i].toString() %></option>
<%										
								}
						
							}
%>					
						</select>
					</td> 
					<td>&nbsp;</td>
				</tr>
<%
				if(!errAdministration.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errAdministration %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%>  
				<tr> 
					<td style="text-align:center;font-weight:bold;">Catégorie thérapeutique * :</td> 
					<td style="text-align:left;">
						<select name="categorieTherapeutique">
							<option value=""></option>
<%
							for(int i=0;i<tabCategorieTherapeutique.length;i++) {
								if(categorieTherapeutique.trim().equals(tabCategorieTherapeutique[i].toString())) {
%>
								 	<option value="<%= tabCategorieTherapeutique[i].toString() %>" selected="selected"><%= tabCategorieTherapeutique[i].toString() %></option>
<%		
								}else{
%>
									<option value="<%= tabCategorieTherapeutique[i].toString() %>"><%= tabCategorieTherapeutique[i].toString() %></option>
<%										
								}
							}
%>					
						</select>
					</td>  
					<td>&nbsp;</td>
				</tr> 
<%
				if(!errCategorieTherapeutique.trim().equals("")) {
%>		
					<tr>
						<td>&nbsp;</td>
						<td id="erreur"><%= errCategorieTherapeutique %></td>
						<td>&nbsp;</td>
					</tr>
<%
				}
%>
				<tr> 
					<td>&nbsp;</td> 
					<td>&nbsp;</td> 
				</tr> 
				<tr> 
					<td>&nbsp;</td> 
					<td id="erreur"><%= errAjout %></td>
					<td  style="text-align:right;"><input type="submit" name="submit" value="Enregistrer le médicament" /></td> 
				</tr> 
				<tr> 
					<td style="font-style:italic;font-size:10px;">* : champ obligatoire</td> 
					<td>&nbsp;</td> 
					<td>&nbsp;</td>
				</tr> 
			</table> 
		</form>
</div>
<div id="contenu_style2_bas">
</div>